package org.utils.net;

import java.io.IOException;

public class NetworkUtils
{
	private NetworkUtils()
	{
		super();
	}
	
	public static String getOperatingSystemName()
	{
		return System.getProperty("os.name");
	}

	public static String getIPAdress() throws IOException
	{
		return java.net.InetAddress.getLocalHost().getHostAddress();
	}

	public static String getHostname() throws IOException
	{
		return java.net.InetAddress.getLocalHost().getHostName();
	}

	/** Sort of like a factory... */
	public static NetworkInfo getNetworkInfo() throws IOException
	{
		String os = getOperatingSystemName();

		if (os.startsWith("Windows"))
		{
			return new WindowsNetworkInfo();
		}
		else if (os.startsWith("Linux"))
		{
			return new LinuxNetworkInfo();
		}
		else
		{
			throw new IOException("Unknown operating system: " + os);
		}
	}

	/** Test method */
	public final static void main(String[] args)
	{
		try
		{
			System.out.println("Network infos");
			System.out.println("  Operating System: " + NetworkUtils.getOperatingSystemName());
			System.out.println("  Hostname: " + NetworkUtils.getHostname());
			System.out.println("  IP/Localhost: " + NetworkUtils.getIPAdress());
			NetworkInfo info = NetworkUtils.getNetworkInfo();
			System.out.println("  MAC Address: " + info.getMacAddress());
			System.out.println("  Domain: " + info.getNetworkDomain());
		}
		catch (Throwable t)
		{
			t.printStackTrace();
		}
	}
}
